<?php
class Comments_model extends Model {
	
    function Comments_model()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    function get($piece_id){
    	
    	$this->db->select('*');
    	$this->db->from('comments');
    	$this->db->where('piece_id', $piece_id);
    	$this->db->join('users_auth AS u', 'u.id = comments.user_id');
    	$this->db->order_by("date", "desc");
    	$query = $this->db->get();
    	return $query->result();
    }
    
    function get_last($n){
    	
    	$this->db->select('username, p.name as piecename, p.id as pieceid, comment, date');
    	$this->db->from('comments');
    	$this->db->join('users_auth AS u', 'u.id = comments.user_id');
    	$this->db->join('pieces AS p', 'p.id = comments.piece_id');
    	$this->db->order_by("date", "desc");
    	//$this->db->limit($n);
    	$query = $this->db->get();
    	return $query->result();
    }
    
    function insert($data){
    	$data = $_POST;
    	$data['date'] = date("Y-m-d"); 
    	$this->db->insert('comments', $data);
    }
	
}